<!--
 * @Author: 许前浩
 * @Date: 2023-04-20 09:07:18
 * @LastEditors: 许前浩
 * @LastEditTime: 2023-04-21 10:56:59
 * @Description: file content
 * @FilePath: \ZR.Vue\src\views\login.vue
-->
<template>
  <div class="login">
    <!-- <div class="welcome">
      <el-image :src="DoanIcon" :fit="fit" style="width: 40px; height: 35px; margin: 0 atuo"> </el-image>
      {{ title }}
    </div> -->
    <div class="bluebackground">
      <div style="width: 50%">
        <div style="height: 90px"></div>
        <div class="bigtitle">MES管理系统</div>
        <div class="smallTitle">
          上海干巷车镜实业有限公司创建于1970年开始生产汽车、摩托车后视镜。经过三十多年的艰苦创业和技术改造，现已成为国内专业生产各种型号汽车后视镜行业骨干企业。
        </div>
      </div>

      <div class="contain">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <div style="height: 30px"></div>

          <h3 class="title">
            <svg class="icon_title" aria-hidden="true">
              <use xlink:href="#icon-gongyezidonghua" /></svg
            >用户登陆
          </h3>
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码" @keyup.enter.native="handleLogin">
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="showCaptcha != 'off'">
            <el-input
              v-model="loginForm.code"
              auto-complete="off"
              placeholder="验证码"
              style="width: 63%"
              @keyup.enter.native="handleLogin"
              ref="codeTxt"
            >
              <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img" />
            </div>
          </el-form-item>
          <el-checkbox v-model="loginForm.rememberMe" style="margin: 0px 0px 25px 0px">记住密码</el-checkbox>
          <el-form-item style="width: 100%; margin: auto 0">
            <el-button :loading="loading" size="medium" round style="width: 90%; margin: auto 0" @click.native.prevent="handleLogin">
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>

            <div style="float: right">
              <router-link class="link-type" :to="'/register'">还没有账号？立即注册</router-link>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>

    <!--  底部  -->
    <div class="el-login-footer">
      <span>{{ copyRight }}</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from '@/api/system/login'
import Cookies from 'js-cookie'
import defaultSettings from '@/settings'
import { encrypt, decrypt } from '@/utils/jsencrypt'
import imageUrl from '@/assets/image/login-pic03.png'
import DoanIcon from '@/assets/logo/logo.png'
import '@/assets/iconfont/login/iconfont.js'

export default {
  name: 'Login',
  data() {
    return {
      DoanIcon: DoanIcon,
      imageUrl: imageUrl,
      codeUrl: '',
      cookiePassword: '',
      loginForm: {
        username: '',
        password: '',
        rememberMe: false,
        code: '',
        uuid: '',
      },
      title: defaultSettings.title,
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '用户名不能为空' }],
        password: [{ required: true, trigger: 'blur', message: '密码不能为空' }],
        code: [{ required: true, trigger: 'change', message: '验证码不能为空' }],
      },
      showCaptcha: '',
      loading: false,
      redirect: undefined,
    }
  },
  computed: {
    copyRight: function () {
      return defaultSettings.copyRight
    },
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true,
    },
  },
  created() {
    this.getCode()
    this.getCookie()
    // this.getConfigKey('sys.account.captchaOnOff').then((response) => {
    //   this.showCaptcha = response.data
    // })
  },
  methods: {
    getCode() {
      // this.loginForm.code = "";
      getCodeImg().then((res) => {
        this.codeUrl = 'data:image/gif;base64,' + res.data.img
        this.loginForm.uuid = res.data.uuid
        this.showCaptcha = res.data.captchaOff
        this.$forceUpdate()
      })
    },
    getCookie() {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')

      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set('username', this.loginForm.username, { expires: 30 })
            Cookies.set('password', encrypt(this.loginForm.password), {
              expires: 30,
            })
            Cookies.set('rememberMe', this.loginForm.rememberMe, {
              expires: 30,
            })
          } else {
            Cookies.remove('username')
            Cookies.remove('password')
            Cookies.remove('rememberMe')
          }
          this.$store
            .dispatch('Login', this.loginForm)
            .then(() => {
              this.msgSuccess('登录成功')
              this.$router.push({ path: this.redirect || '/' })
            })
            .catch((error) => {
              this.msgError(error.msg)
              this.loading = false
              this.getCode()
              this.$refs.codeTxt.focus()
            })
        } else {
          console.log('未完成')
        }
      })
    },
  },
}
</script>

<style scoped rel="stylesheet/scss" lang="scss" scoped>
.login {
  position: fixed;
  height: 100%;
  width: 100%;
  background-image: url('../assets/logo/background.png');
  //background: #141e47;
  background-size: cover;
  // background-color: rgba(56, 157, 170, 0.82);
  .bluebackground {
    height: 320px;
    width: 600px;
    background: #203589;
    border-radius: 10px;
    margin-left: 50%;
    margin-top: 13%;
    .bigtitle {
      color: #fff;
      font-size: 25px;
      font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', '微软雅黑', Arial, sans-serif;
      margin-left: 10px;
      margin-bottom: 30px;
    }
    .smallTitle {
      color: #fff;
      font-size: 12px;
      font-family: '幼圆';
      margin-left: 10px;
      width: 250px;
    }
    .contain {
      position: relative;
      border-radius: 6px;
      top: -240px;
      left: 270px;
      background: #fff;
      width: 300px;
      height: 420px;
      .login-form {
        border-radius: 6px;
        height: inherit;
        width: 90%;
        margin: 0 auto;
        .el-input {
          height: 38px;

          input {
            height: 38px;
          }
        }
      }
    }
  }

  .welcome {
    width: 880px;
    margin: 0px auto 50px;
    text-align: center;
    font-size: 28px;
    font-weight: bold;
    color: #f6f0f0;
  }

  .title {
    margin: 0px auto 30px auto;
    text-align: center;
    // color: #707070;
    color: #203589;
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
  width: 80px;
}
.icon_title {
  width: 2em;
  height: 2em;
  vertical-align: -0.15em;
  overflow: hidden;
}
</style>
